package com.xf.controller;

import com.xf.entity.SysUser;
import com.xf.entity.User;
import com.xf.mapper.PersonDao;
import com.xf.service.BlindService;
import com.xf.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/blind")
@CrossOrigin
public class BlindController {
    private boolean scanned = false;

    @Autowired
    private BlindService service;

    @Autowired
    private PersonService personService;

    @Autowired
    private PersonDao dao;

    @RequestMapping("getUser/{id}")
    public User GetUser(@PathVariable int id) {
        return service.queryById(id);
    }

    @RequestMapping("/page")
    public ModelAndView myPage() {
        ModelAndView mv =  new ModelAndView("index");
        return mv; // 这里返回的字符串对应你的HTML页面文件名
    }

    @RequestMapping("/home")
    public ModelAndView home() {
        ModelAndView mv =  new ModelAndView("home");
        return mv; // 这里返回的字符串对应你的HTML页面文件名
    }



    @RequestMapping("/findAll")
    public List<SysUser> findAll() {
        return personService.findAll();
    }

    @RequestMapping("/findById/{id}")
    public SysUser findById(@PathVariable Long id) {
        return personService.findById(id);
    }

    @RequestMapping("/saveSysUser")
    public ModelAndView insert(@RequestBody SysUser sysUser) {
        int insert = personService.insert(sysUser);
        if (insert > 0) {
            return  new ModelAndView("success"); // 这里返回的字符串对应你的HTML页面文件名
        }
        return  new ModelAndView("error");

    }

    @RequestMapping("/update")
    public void update(@RequestBody SysUser sysUser) {
        personService.update(sysUser);
    }

    @RequestMapping("/delete/{id}")
    public void delete(@PathVariable Long id) {
        personService.delete(id);
    }

    @RequestMapping("/success")
    public ModelAndView success() {
        ModelAndView mv =  new ModelAndView("success");
        return mv; // 这里返回的字符串对应你的HTML页面文件名
    }

    @RequestMapping("/error")
    public ModelAndView error() {
        ModelAndView mv =  new ModelAndView("error");
        return mv; // 这里返回的字符串对应你的HTML页面文件名
    }

    @GetMapping("/{id}")
    public SysUser getUserById(@PathVariable Long id) {
        return dao.getUserById(id);
    }

}
